<template>
  <div class="mod-reve-profile">
    <!-- 标题 -->
    <div class="header">
      <h3>{{$t('finaManage.revenueOverview')}}</h3>
    </div>

    <!-- 时间选择栏 -->
    <div class="time-header">
      <div class="time-container">
        <div class="time">
          <el-select
            v-model="dateValue"
            @change="setDateRange(dateValue)"
            style="width:110px;height: 15px;"
            size="mini"
          >
            <el-option
              v-for="item in dateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <span class="title-time" v-if="dateValue === 1 || dateValue === 2">{{day}}</span>
          <span
            class="title-time"
            v-if="dateValue === 3 || dateValue === 4 || dateValue === 5 || dateValue === 6"
          >{{startTime}} {{$t('date.tip')}} {{endTime}}</span>
          <span class="title-time title-time-span" v-if="dateValue === 7">
            <el-date-picker
              class="title-time-Range"
              v-model="timeRange"
              type="daterange"
              align="center"
              unlink-panels
              range-separator="-"
              @change="changeDateTime"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
              value-format="timestamp"
            >></el-date-picker>
          </span>
          <div class="title-picker"></div>
        </div>

        <!-- 头部下拉选框 -->
        <div class="dropDowm">
          <el-select v-model="storeValue" style="width:160px;height: 15px;" size="mini">
            <el-option
              v-for="item in dropDowmOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <span>
            <el-tooltip placement="top" effect="light">
              <div slot="content">
                {{$t('finaManage.theDataShows')}}：
                <br/>{{$t('finaManage.tip1')}}
                <br/>{{$t('finaManage.tip2')}}
                <br/>{{$t('finaManage.tip3')}}
              </div>
              <i class="el-icon-question"></i>
            </el-tooltip>
          </span>
        </div>
      </div>
      <div class="time-text">
        <p>{{$t('finaManage.tip4')}}：</p>
        <p class="p">{{$t('finaManage.tip5')}}</p>
        <p class="p">{{$t('finaManage.tip6')}}</p>
      </div>
    </div>
    <!-- end -->

    <!-- 收入概况数据展示 -->
    <div class="profile">
      <h4>{{$t('finaManage.incProfile')}}</h4>
      <div class="profile-data">
        <el-row>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <div class="grid-header">{{$t('finaManage.actIncome')}}</div>
              <div class="grid-num">
                <span>{{actualIncome}}</span>{{$t('dataAnalysis.yuan')}}
              </div>
              <div class="grid-numb">
                {{$t('finaManage.chainRatio')}}
                <span v-if="actualIncomeRatio === 0 ">{{$t('order.notYet')}}</span>
                <span v-else>
                  {{actualIncomeRatio}}%
                  <i class="el-icon-top" v-if="actualIncomeRatio > 0 "></i>
                  <i class="el-icon-bottom" v-if="actualIncomeRatio < 0 "></i>
                </span>
              </div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple-light">
              <div class="grid-header">{{$t('finaManage.incAmount')}}</div>
              <div class="grid-num">
                <span>{{incomeAmount}}</span>{{$t('dataAnalysis.yuan')}}
              </div>
              <div class="grid-numb">
                {{$t('finaManage.chainRatio')}}
                <span v-if="incomeAmountRatio === 0 ">{{$t('order.notYet')}}</span>
                <span v-else>
                  {{incomeAmountRatio}}%
                  <i class="el-icon-top" v-if="incomeAmountRatio > 0 "></i>
                  <i class="el-icon-bottom" v-if="incomeAmountRatio < 0 "></i>
                </span>
              </div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="grid-content bg-purple">
              <div class="grid-header">{{$t('finaManage.amountPayout')}}</div>
              <div class="grid-num">
                <span>{{payoutAmount}}</span>{{$t('dataAnalysis.yuan')}}
              </div>
              <div class="grid-numb">
                {{$t('finaManage.chainRatio')}}
                <span v-if="payoutAmountRatio === 0 ">{{$t('order.notYet')}}</span>
                <span v-else>
                  {{payoutAmountRatio}}%
                  <i class="el-icon-top" v-if="payoutAmountRatio > 0 "></i>
                  <i class="el-icon-bottom" v-if="payoutAmountRatio < 0 "></i>
                </span>
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <!-- end -->

    <!-- 营收趋势数据展示 -->
    <div class="profile trend">
      <h4>{{$t('finaManage.reveTrend')}}</h4>
      <div class="trend-echart">
        <div id="trend-echart" style="width:100%;height:400px;"></div>
      </div>
    </div>
  </div>
  <!-- end -->
</template>

<script>
  import moment from 'moment'
  export default {
    data () {
      return {
        dateValue: 1, //  时间栏默认选中
        dateRange: [], // 展示的时间参数
        timeRange: '',
        startTime: '',
        endTime: '',
        day: '',  //  今天，昨天选中展示

        dateOptions: [
          {
            label: this.$i18n.t('user.today'),
            value: 1
          },
          {
            label: this.$i18n.t('user.yesterday'),
            value: 2
          },
          {
            label: this.$i18n.t('user.lastSevenDay'),
            value: 3
          },
          {
            label: this.$i18n.t('user.lastThirtyDay'),
            value: 4
          },
          {
            label: this.$i18n.t('user.thisMonth'),
            value: 5
          },
          {
            label: this.$i18n.t('user.lastMonth'),
            value: 6
          },
          {
            label: this.$i18n.t('user.customRange'),
            value: 7
          }
        ],

        storeValue: -1, //  门店下拉的选项
        dropDowmOptions: [
          {
            label: this.$i18n.t('date.a'),
            value: -1
          }
        ],
        actualIncome: '',
        actualIncomeRatio: '',
        incomeAmount: '',
        incomeAmountRatio: '',
        payoutAmount: '',
        payoutAmountRatio: '',
        trendDataList: {
          currentDate: [],
          incomeAmount: [],
          payoutAmount: [],
          actualIncome: []
        }
      }
    },
    mounted () {
      this.getShopList()
      this.setDateRange(3)
    },
    watch: {
      storeValue (val) {
        this.updateDateTime()
        this.getRevenueData()
      }
    },
    methods: {
      getRevenueData () {
        // 获取营收概况页面数据
        this.$http({
          url: this.$http.adornUrl(`/platform/financialManagement/getIncomeProfile`),
          method: 'get',
          params: this.$http.adornParams({
            typeType: this.dateValue,
            start: this.dateRange ? this.dateRange[0] : null,
            end: this.dateRange ? this.dateRange[1] : null,
            stationId: this.storeValue
          })
        }).then(({data}) => {
          if (data) {
            this.tableData = data
            this.actualIncome = data.actualIncome
            this.actualIncomeRatio = data.actualIncomeRatio
            this.incomeAmount = data.incomeAmount
            this.incomeAmountRatio = data.incomeAmountRatio
            this.payoutAmount = data.payoutAmount
            this.payoutAmountRatio = data.payoutAmountRatio
            const trendDataList = data.trendDataList
            trendDataList.forEach(item => {
              this.trendDataList.currentDate.push(item.currentDate)
              this.trendDataList.incomeAmount.push(item.incomeAmount)
              this.trendDataList.payoutAmount.push(item.payoutAmount)
              this.trendDataList.actualIncome.push(item.actualIncome)
            })
            if (trendDataList.length === 0) {
            }
            this.getTrendEchart()
          }
        })
      },
      updateDateTime () {
        this.trendDataList = {
          currentDate: [],
          incomeAmount: [],
          payoutAmount: [],
          actualIncome: []
        }
      },
      // 1-7:今日、昨日、最近7天、最近30天、这个月、上个月以及自定义时间,都是2020-8-20模式
      setDateRange (val) {
        this.dateValue = val
        var date = new Date()
        if (val === 1) {
          // 今天
          this.day = moment(date).format('L')
          this.startTime = this.day
          this.endTime = this.day
        } else if (val === 2) {
          // 昨天
          this.day = moment(date).add(-1, 'days').format('L')
          this.startTime = this.day
          this.endTime = this.day
        } else if (val === 3) {
          // 近7天
          this.endTime = moment(date).add(-1, 'days').format('L')
          this.startTime = moment(date).add(-7, 'days').format('L')
        } else if (val === 4) {
          // 近30天
          this.endTime = moment(date).add(-1, 'days').format('L')
          this.startTime = moment(date).add(-30, 'days').format('L')
        } else if (val === 5) {
          // 这个月
          this.endTime = moment(date).format('L')
          this.startTime = moment(date).startOf('month').format('L')
        } else if (val === 6) {
          // 上个月
          let month = moment(date).add(-1, 'month').startOf('month')
          this.endTime = moment(month).endOf('month').format('L')
          this.startTime = month.format('L')
        } else if (val === 7) {
          return
        } else {
          return
        }
        var startTimeStamp = moment(this.startTime).unix() * 1000
        var endTimeStamp = moment(this.endTime).unix() * 1000
        this.dateRange = [startTimeStamp, endTimeStamp]
        this.updateDateTime()
        this.getRevenueData()
      },
      // 自定义时间栏，改变时调用
      changeDateTime (timeRange) {
        var startTimeStamp = timeRange[0]
        var endTimeStamp = timeRange[1]
        this.dateRange = [startTimeStamp, endTimeStamp]
        this.updateDateTime()
        this.getRevenueData()
      },
      // 趋势图
      getTrendEchart () {
        var dom = document.getElementById('trend-echart')
        var w3 = window.getComputedStyle(dom).width
        document.getElementById('trend-echart').style.width = w3
        const myChart = this.$echarts.init(document.getElementById('trend-echart'), 'walden')
        const option = {
          title: {
            // text: '折线图堆叠'
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: [this.$i18n.t('finaManage.actIncome'), this.$i18n.t('finaManage.incAmount'), this.$i18n.t('finaManage.amountPayout')]
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.trendDataList.currentDate
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: this.$i18n.t('finaManage.actIncome'),
              type: 'line',
              data: this.trendDataList.actualIncome
            },
            {
              name: this.$i18n.t('finaManage.incAmount'),
              type: 'line',
              data: this.trendDataList.incomeAmount
            },
            {
              name: this.$i18n.t('finaManage.amountPayout'),
              type: 'line',
              data: this.trendDataList.payoutAmount
            }
          ]
        }
        myChart.setOption(option)
        window.addEventListener('resize', function () {
          myChart.resize()
        })
      },
      getShopList () {
        // 获取营收概况页面数据
        this.$http({
          url: this.$http.adornUrl(`/platform/financialManagement/getStation`),
          method: 'get',
          params: this.$http.adornParams({
            shopId: 1
          })
        }).then(({data}) => {
          if (data) {
            for (let i = 0; i < data.length; i++) {
              this.dropDowmOptions.push({
                label: data[i].stationName,
                value: data[i].stationId
              })
            }
            this.dropDowmOptions.push({
              label: this.$i18n.t('prodList.other'),
              value: -2
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .mod-reve-profile {
    .header {
      margin: 10px 0;
      margin-bottom: 28px;
    }

    .header h3 {
      color: #4d4d4d;
      font-weight: 580;
    }

    .time-header {
      margin: 10px 0;
      background-color: #f8f8f8;
      padding-left: 10px;
      padding-bottom: 10px;
      border-radius: 6px;
    }

    .time-header .time-container {
      height: 80px;
      border-bottom: 1px #bebebe solid;
      margin: 10px 0;
      padding: 10px 0;
      padding-top: 28px;
      position: relative;
    }

    .time-header .time-container .time {
      display: inline-block;
    }

    .time-header .time-container .dropDowm {
      display: inline-block;
      padding-left: 2%;
    }

    .time-header .time-container .radio {
      position: absolute;
      top: 28px;
      right: 80px;
      display: inline-block;
    }

    .title-time {
      padding-left: 8px;
      color: #8f8f8f;
    }

    .title-time-span {
      .el-date-editor .el-range__icon {
        line-height: 22px;
      }

      .el-date-editor .el-range-separator {
        width: 6%;
        line-height: 24px;
      }

      .el-date-editor .el-range__close-icon {
        line-height: 24px;
      }
    }

    .title-time-Range {
      height: 30px;
    }

    .time-header .time-text {
      padding-top: 6px;
      color: #747474;
      line-height: 24px;
    }

    .time-header .time-text .p {
      padding-left: 90px;
    }

    .profile {
      margin: 10px 0;
      background-color: #f8f8f8;
      padding: 10px;
      border-radius: 6px;
    }

    .profile h4 {
      color: #4d4d4d;
      font-weight: 580;
      line-height: 40px;
      border-bottom: 1px #bebebe solid;
    }

    .profile .profile-data {
      height: 140px;
      padding: 20px;
      text-align: center;
      line-height: 30px;
      margin-top: 20px;
    }

    .profile .profile-data .grid-header {
      color: #585858;
      font-weight: 600;
    }

    .profile .profile-data .grid-num span {
      color: #444444;
      font-weight: 800;
      font-size: 20px;
    }

    .profile .profile-data .grid-numb {
      color: #777777;
      font-size: 12px;
    }

    .profile .profile-data .grid-numb span {
      color: #d88e05;
      font-size: 10px;
    }

    .trend .trend-echart {
      height: 400px;
      padding: 20px;
    }
  }
</style>

